var express = require('express');

var router = express.Router();
var User = require('../models/Users')
var Content = require('../models/Content')
//统一返回格式
var responseData;
router.use(function (req, res, next) {
    responseData = {
        code: 0,
        message: ""
    }
    next();
})


/*
 *用户注册
 * 逻辑
 *1.用户名不能空 1.密码不能空 1.两次密码一致
 *2.用户是否被注册(数据库查询)
 */
router.post("/user/register", function (req, res, next) {

    var username = req.body.username;
    var password = req.body.password;
    var repassword = req.body.repassword;
    // 用户名不能空
    if (username == '') {
        responseData = {
            code: 1,
            message: "用户名不能空"
        };
        res.json(responseData);
        return;
    }
    // 密码不能空
    if (password == '') {
        responseData = {
            code: 2,
            message: "密码不能空"
        };
        res.json(responseData);
        return;
    }
    if (password != repassword) {
        responseData = {
            code: 3,
            message: "两次密码输入不一致"
        };
        res.json(responseData);
        return;
    }

    // 数据库查询用户是否被注册
    User.findOne({
        username: username
    }).then(function (userInfo) {
        console.log(userInfo);
        if (userInfo) {
            //存在
            responseData = {
                code: 4,
                message: "用户名已被注册"
            };
            res.json(responseData);
            return;
        } else {
            // 保存用户信息到数据库
            var user = new User({
                username: username,
                password: password
            })
            return user.save();
        }
    }).then(function (newUserInfo) {
        console.log(newUserInfo);
        responseData = {
            code: 0,
            message: "注册成功"
        };
        res.json(responseData);
    })


})

router.post('/user/login',function(req,res,next){
    var username = req.body.username;
    var password = req.body.password;
    if(username=='' || password==''){
        responseData = {
            code: 1,
            message: "用户名密码不能为空"
        }
        res.json(responseData);
        return;
    }
    // 查询数据库相同的用户名和密码的记录是否存在
    User.findOne({
        username:username,
        password:password
    }).then(function(userInfo){
         if(!userInfo){
            responseData = {
                code: 2,
                message: "用户名密码错误"
            }
            res.json(responseData);
            return;
         }else {
            responseData = {
                code: 0,
                message: "登录成功",
                userInfo:{
                    _id :userInfo._id,
                    username:userInfo.username
                }
            }
            // 发送cookies信息给浏览器
            req.cookies.set('userInfo',JSON.stringify({
                _id :userInfo._id,
                username:userInfo.username
            }));
            res.json(responseData);
            return;
         }
    })
})

// 退出
router.get("/user/logout",function(req,res){
      req.cookies.set('userInfo',null);
      res.json(responseData);
})

//获取评论

router.get('/comment',function (req,res) {
    var contentId = req.query.contentid || '';//内容id
    Content.findOne({
        _id:contentId
    }).then(function (content) {
        responseData.data = content.comments;
        res.json(responseData);
    })
})

//提交评论
router.post('/comment/post',function (req,res) {
    var contentId = req.body.contentid || '';//内容id
    var postData={
         username : req.userInfo.username,
         postTime:new Date(),
         contentVal:req.body.content,
    }
    //查询当前这篇内容的信息
    Content.findOne({
        _id:contentId
    }).then(function (content) {
        content.comments.push(postData);
        return content.save();
    }).then(function (newContent) {
        responseData.message = '评论成功';
        console.log(newContent)
        responseData.data = newContent;
        res.json(responseData);
    })
})


module.exports = router;